package cn.manter.user.service.impl;

import cn.manter.basic.dto.EmailDto;
import cn.manter.basic.dto.SmsCodeBto;
import cn.manter.basic.constant.BaseConstants;
import cn.manter.basic.exception.BusinessException;
import cn.manter.basic.utils.MD5Utils;
import cn.manter.basic.utils.StrUtils;
import cn.manter.user.domain.Logininfo;
import cn.manter.user.domain.User;
import cn.manter.user.mapper.LogininfoMapper;
import cn.manter.user.mapper.UserMapper;
import cn.manter.user.service.IUserService;
import cn.manter.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-07-05
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(SmsCodeBto smsCodeBto) {
        // 0.获取到参数
        String phone = smsCodeBto.getPhone();
        String phoneCodeValue = smsCodeBto.getPhoneCodeValue();
        String password = smsCodeBto.getPassword();
        String confirmPassword = smsCodeBto.getConfirmPassword();
        // 1.判断参数非空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCodeValue)
                || StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(password)){
            throw new BusinessException("参数不能为空，请重新输入");
        }

        // 2.判断两次手机号是否一致
        String phoneCodeKey = BaseConstants.VerifyCodeConstant.BUSINESS_REGISTER_KEY + phone;
        Object phoneCodeValueRedis = redisTemplate.opsForValue().get(phoneCodeKey);
        if(phoneCodeValueRedis == null){
            throw new BusinessException("验证码已过期，请重新获取！");
        }

        // 3.判断验证码是否正确
        // 3.1.获取到分割后的验证码
        String phoneCode = phoneCodeValueRedis.toString().split(":")[1];
        if (!phoneCode.equalsIgnoreCase(phoneCodeValue)){
            throw new BusinessException("手机验证码错误，请重新输入！");
        }

        // 4判断两次密码是否一致
        if(!password.equals(confirmPassword)){
            throw new BusinessException("两次密码输入不一致，请重新输入！");
        }

        // 5.判断用户是否已注册，如果已注册提示用户
        User user = userMapper.loadByPhone(phone);
        if(user != null){
            throw new BusinessException("用户名已注册，请直接登录，如果忘记，请点击忘记密码！");
        }

        // 6.初始化user表,就是对你的user对象进行赋值操作
        user = smsCodeDto2User(smsCodeBto);
        // 6.1 保存user表
        userMapper.save(user);

        // 7.保存logininfo
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);

        // 8.将logininfo主键设置到user表，并修改
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);

    }

    @Override
    public void emailRegister(EmailDto emailDto) {
        // 1.获取参数信息
        String email = emailDto.getEmail();
        String password = emailDto.getPassword();
        String passwordRepeat = emailDto.getPasswordRepeat();
        // 2.判断参数非空
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(passwordRepeat)){
            throw new BusinessException("参数不能为空，请重新输入！");
        }

        // 3.判断两次密码是否一致
        if(!password.equals(passwordRepeat)){
            throw new BusinessException("两次密码输入不一致，请重新输入！");
        }

        // 4.判断邮箱是否注册
        User user = userMapper.loadByEmail(email);
        if(user != null){
            throw new BusinessException("用户名已注册，请直接登录，如果忘记，请点击忘记密码！");
        }

        // 6.初始化user表,就是对你的user对象进行赋值操作
        user = emailDto2User(emailDto);
        // 6.1 保存user表
        userMapper.save(user);

        // 7.保存logininfo
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);

        // 8.将logininfo主键设置到user表，并修改
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);


    }

    /**
    * @Title: user2Logininfo
    * @Description: 将user的值赋值給Logininfo
    * @Author: Mr.Zhao
    * @Version: 1.0
    * @Date:  2022/7/6 0006 18:04
    * @Parameters: [user]
    * @Return cn.manter.user.domain.Logininfo
    */
    private Logininfo user2Logininfo(User user) {
        // 创建Logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);

        return logininfo;
    }

    /**
    * @Title: smsCodeDto2User
    * @Description: 将smsCodeBto的值赋值给user
    * @Author: Mr.Zhao
    * @Version: 1.0
    * @Date:  2022/7/6 0006 18:04
    * @Parameters: [smsCodeBto]
    * @Return cn.manter.user.domain.User
    */
    private User smsCodeDto2User(SmsCodeBto smsCodeBto) {
        // 1.创建一个user对象
        String phone = smsCodeBto.getPhone();
        User user = new User();
        // 1.1设置手机号
        user.setPhone(phone);
        // user的user是手机号
        user.setUsername(phone);

        // 2.加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt + smsCodeBto.getPassword());
        user.setSalt(salt);
        user.setPassword(password);

        return user;
    }
    /**
    * @Title: emailDto2User
    * @Description: 将邮箱注册信息同步到user表中
    * @Author: Mr.Zhao
    * @Version: 1.0
    * @Date:  2022/7/9 0009 13:25
    * @Parameters: [emailDto]
    * @Return cn.manter.user.domain.User
    */
    private User emailDto2User(EmailDto emailDto) {
        // 1.创建一个user对象
        String email = emailDto.getEmail();
        User user = new User();
        // 1.1设置手机号
        user.setEmail(email);
        // user的user是手机号
        user.setUsername(email);

        // 2.加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt + emailDto.getPassword());
        user.setSalt(salt);
        user.setPassword(password);

        return user;
    }
}
